﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BikeSystem.Models;
using BikeSystem.BLL;

namespace BikeUI
{
    public partial class FormBikes : FormBase
    {
        AccountManager accountManager = new AccountManager();
        BikeManager bikeManager = new BikeManager();
        private IList<SystemOption> systemOptions = null;
        public FormBikes()
        {
            InitializeComponent();

            bikeBindingSource.RaiseListChangedEvents = true;

            cbFilterModelType.Items.Add("ALL");
            systemOptions = accountManager.GetAllOptions();
            foreach (SystemOption opt in systemOptions)
            {
                if (ConstData.OPTION_BICYCLE_TYPE.Equals(opt.OptionType))
                {
                    cbFilterModelType.Items.Add(opt.OptionName);
                    cbModelType.Items.Add(opt.OptionName);
                }
                else if (ConstData.OPTION_BICYCLE_RATE_HOURLY.Equals(opt.OptionType))
                {
                    cbHurlyRate.Items.Add(opt.OptionValue);
                }
                else if (ConstData.OPTION_BICYCLE_RATE_DAILY.Equals(opt.OptionType))
                {
                    cbDailyRate.Items.Add(opt.OptionValue);
                }
            }
            cbFilterModelType.SelectedIndex = 0;


            loadBikes();

            UCButtonGroup.KeyComponent = txtBikeNo;
            UCButtonGroup.EditableGroupBox = gbxBike;
            UCButtonGroup.BindingSource = bikeBindingSource;

            UCButtonGroup.EditableGroupBox.Enabled = false;

        }


        public override void CurrentAccount(Account account)
        {
            currentAccount = account;
            UCButtonGroup.Enabled = currentAccount.Role.Equals(ConstData.ACCOUNT_ROLE_ADMIN);
            btnRent.Enabled = currentAccount.Role.Equals(ConstData.ACCOUNT_ROLE_OPERATOR);
        }

        public IList<SystemOption> SystemOptions
        {
            get { return systemOptions; }
            set { systemOptions = value; }
        }

        protected override void btnNew_Click(object sender, EventArgs e)
        {

            Bike bike = (Bike)bikeBindingSource.Current;
            bike.BikeNo = CommonUtils.genrateNextSerialNumber(ConstData.SYSTEM_NEXT_SERIALNUMBER_BIKE);
            bike.IsNew = true;

            txtBikeNo.Text = bike.BikeNo;

        }

        protected override void btnUpdate_Click(object sender, EventArgs e)
        {

            Bike bike = (Bike)bikeBindingSource.Current;

            if (!bike.IsNew)
            {
                bike.IsUpdated = true;
            }


        }

        protected override void btnSave_Click(object sender, EventArgs e)
        {
            errorProviderClear();

            txtAlphaNumericString_Validated(txtBikeNo);

            if (!UCButtonGroup.HasError)
            {
                Bike bike = (Bike)bikeBindingSource.Current;
                if (bike.IsNew)
                {
                    bikeManager.InsertBike(bike);
                }
                else if (bike.IsUpdated)
                {
                    bikeManager.UpdateBike(bike);
                }
                loadBikes();

            }


        }

        protected override void btnDelete_Click(object sender, EventArgs e)
        {

            Bike bike = (Bike)bikeBindingSource.Current;

            if (!bike.IsNew)
            {
                bikeManager.DeleteBike(bike.BikeNo);
            }

            loadBikes();
        }


        private void loadBikes()
        {

            IList<Bike> bikes = bikeManager.GetAllBikes();


            double highRate = 0;
            double lowRate = 0;
            if (!string.IsNullOrEmpty(txtFilterLow.Text))
            {
                lowRate = double.Parse(txtFilterLow.Text);
            }
            if (!string.IsNullOrEmpty(txtFilterHigh.Text))
            {
                highRate = double.Parse(txtFilterHigh.Text);
            }

            IList<Bike> listBikes = new List<Bike>();


            //listBikes.Where(p => p.BikeNo.StartsWith("App1"));

            foreach (Bike bike in bikes)
            {

                if (bike.Status == cbFilterStatus.Checked
                    && (txtRangeValidationIsValid(bike.DailyRate.ToString(), lowRate, highRate) || txtRangeValidationIsValid(bike.HourlyRate.ToString(), lowRate, highRate)))
                {
                    if ("ALL".Equals(cbFilterModelType.Text))
                    {
                        listBikes.Add(bike);
                    }
                    else if (bike.ModelType.Equals(cbFilterModelType.Text))
                    {
                        listBikes.Add(bike);
                    }

                }

            }
            bikeBindingSource.DataSource = listBikes;


        }


        private void cbModelType_SelectedValueChanged(object sender, EventArgs e)
        {

            cbHurlyRate.Items.Clear();
            cbDailyRate.Items.Clear();
            foreach (SystemOption opt in systemOptions)
            {
                if (cbModelType.Text.Equals(opt.OptionName))
                {
                    if (ConstData.OPTION_BICYCLE_RATE_HOURLY.Equals(opt.OptionType))
                    {
                        cbHurlyRate.Items.Add(opt.OptionValue);
                    }
                    else if (ConstData.OPTION_BICYCLE_RATE_DAILY.Equals(opt.OptionType))
                    {
                        cbDailyRate.Items.Add(opt.OptionValue);
                    }
                }
            }
        }

        private void cbFilterModelType_SelectedValueChanged(object sender, EventArgs e)
        {
            loadBikes();
        }

        private void txtFilterLow_TextChanged(object sender, EventArgs e)
        {
            txtNumericString_Validated(sender);
            if (!UCButtonGroup.HasError)
            {
                loadBikes();
            }
        }

        private void txtFilterHigh_TextChanged(object sender, EventArgs e)
        {
            txtNumericString_Validated(sender);
            if (!UCButtonGroup.HasError)
            {
                loadBikes();
            }
        }

        private void cbFilterStatus_CheckedChanged(object sender, EventArgs e)
        {
            loadBikes();
        }

        private void cbStatus_CheckedChanged(object sender, EventArgs e)
        {

            Bike bike = (Bike)bikeBindingSource.Current;
            bike.Status = cbStatus.Checked;
        }

        private void btnRent_Click(object sender, EventArgs e)
        {

            FormRental formRental = new FormRental();

            Bike bike = (Bike)bikeBindingSource.Current;

            Rental rental = new Rental();
            rental.BikeNo = bike.BikeNo;
            rental.OperatorName = currentAccount.UserName;
            rental.RentNo = CommonUtils.genrateNextSerialNumber(ConstData.SYSTEM_NEXT_SERIALNUMBER_RENTAL);
            formRental.BindingSource.Add(rental);

            formRental.Show();

        }
    }
}
